<?php 

namespace Home\Controller;
use Think\Controller;

/**
*  前台登录控制器
*/
class LoginController extends Controller
{
	
	public function index(){
		//var_dump('111');exit;
		if(isset($_SESSION['uid'])){
			$url=U('home/UserCenter/index');
			header('Location:'.$url);
			exit;
		}
		$question= D('vercode')->getOne();

		$this->assign('question',$question);
		$this->assign('title','登录');
		$this -> display();
	}

	public function checkdata(){
		$data = I('post.');
		$answer = $data['vercode'];
		$res= D('vercode')->checkCode($answer);
		$user = D('User');
		//$checkEmail = $user->where(array('email'=>$data['email']))->find();
		//var_dump($checkEmail['password']);exit;
		if(!$res){
			$this->ajaxReturn(["error"=>1,"info"=>'人类验证错误']);
		}

		if(!$user->create($data,4)){
			$this->ajaxReturn(["error"=>1,"info"=>$user->getError()]);
		}else{
			$check = $user->where(array('email'=>$data['email']))->find();
			if(!$check){
				$this->ajaxReturn(["error"=>1,"info"=>'邮箱不存在']);
			}else{
				$password=$check['password'];
				if(!($password == md5($data['password']))){
					$this->ajaxReturn(["error"=>1,"info"=>'密码错误']);
				}else{
					//var_dump($check['status']);exit;
					if($check['status']=='1'){
						$this->ajaxReturn(["error"=>2,"info"=>'账户停用，请与管理员联系']);
					}
					$_SESSION['uid'] = $check['uid'];
					$_SESSION['nickname'] = $check['nickname'];;
					$_SESSION['password'] = $check['password'];
					$_SESSION['face'] = $check['face'];
					$_SESSION['msg'] = $check['msg'];
					$this->ajaxReturn(["error"=>0,"info"=>'登录成功']);
				}
			}
		}
	}

}

 ?>